<?php
define("DIR_ROOT", dirname(dirname(dirname(dirname(__FILE__)))));

$includes = array("ImageModifier", "Config/INITag", "Config/Reader", "Config/Cache");
foreach ($includes as $include){
	if (!include_once(DIR_ROOT . '/libraries/Util/'.$include.'.php')) {
		die;
	}
}

/**
 * On the fly resizing
 * @author Sitemakers LTD
 * @author Somlyai Dezső
 * @author Vincze Tamas pilou@pilou.hu
 * @version 1.1
 */
class OnTheFlyResize {

	/**
	 * On the fly resizing
	 * @access public
	 * @return void
	 */
	public function __construct() {
		$pathInfo = pathinfo($_SERVER["REQUEST_URI"]);
		$documentRoot = rtrim($_SERVER["DOCUMENT_ROOT"], "/");

		$dirName = dirname($pathInfo["dirname"]);
		$dirName = (strpos($dirName, "/media") === 0) ? $dirName : "/media" . $dirName;
		$sourceFilePath = $documentRoot . $dirName . "/" . $pathInfo["basename"];
		if (!file_exists($sourceFilePath)) {
			$this->_imageNotFound("onTheFlyResize - source not exists: " . $sourceFilePath);
		}

		$matches = array();
		if (!preg_match("!^([0-9]+)x([0-9]+)(_|)(fill|crop|wfill|tfill|resize|forceresize|)(_wm)?$!i", basename($pathInfo["dirname"]), $matches)) {
			$this->_imageNotFound("onTheFlyResize - preg match not found:" . basename($pathInfo["dirname"]));
		}
		
		$reader = \Util\Config\Cache::getConfig(DIR_ROOT . "/config/Config.ini");
		
		if (!is_null($reader->IMAGEDISALLOW) && $reader->IMAGEDISALLOW->enabled == true) {
			
			if ($reader->IMAGEDISALLOW->maxWidth){
				$disallow = $matches[1] > $reader->IMAGEDISALLOW->maxWidth;
			} elseif ($reader->IMAGEDISALLOW->maxHeight){
				$disallow = $matches[1] > $reader->IMAGEDISALLOW->maxHeight;
			}
			
			if (isset($disallow) && $disallow === true){ // disallow big images
				header('Content-type: image/png');
				readfile($_SERVER["DOCUMENT_ROOT"] . $reader->IMAGEDISALLOW->imageToOutput);
				die;
			}
		}		

		$newDir = $documentRoot . $dirName . "/" . end(explode("/", $pathInfo["dirname"]));
		$newPath = $newDir . "/" . $pathInfo["basename"];
		if (!is_dir($newDir)) {
			mkdir($newDir, 0777, true);
			chmod($newDir, 0777);
		}

		$img = new \Util\ImageModifier($sourceFilePath);
		$img->newdimension($matches[1], $matches[2], $matches[4]);
		$img->generate($newPath);
		
		if (isset($matches[5])){// watermark
			$img = new \Util\ImageModifier($newPath);
            $img->watermarkFixPosition($_SERVER["DOCUMENT_ROOT"]."/media/images/watermark.png", "top-right",100,20);
			$img->generate($newPath);
		}
		

		switch ($pathInfo["extension"]) {
			case "jpg":
			case "jpeg":
				header('Content-type: image/jpeg');
				break;
			case "png":
				header('Content-type: image/png');
				break;
			case "gif":
				header('Content-type: image/gif');
				break;
			default:
				$this->_imageNotFound("onTheFlyResize - invalid extension:" . $pathInfo["extension"]);
				break;
		}
		header('HTTP/1.1 200 OK');

		readfile($newPath);
		die();
	}

	/**
	 * Sends 404 header if image is not found
	 * @access private
	 * @return void;
	 */
	private function _imageNotFound($error = null) {
		header("HTTP/1.0 404 Not Found");
		die($error);
	}

}

new OnTheFlyResize();
?>